import 'dart:async';
import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_syt/utils/utils_dlog.dart';

class HomeMarqueeView extends StatefulWidget {
  Duration duration; // 轮播时间
  double stepOffset; // 偏移量
  double paddingLeft; // 内容之间的间距
  List<Widget> children = []; //内容
  HomeMarqueeView(
      {required this.paddingLeft,
      required this.duration,
      required this.stepOffset,
      required this.children});
  _BLPointMarqueeViewState createState() => _BLPointMarqueeViewState();
}

class _BLPointMarqueeViewState extends State<HomeMarqueeView> {
  late ScrollController _controller; // 执行动画的controller
  late Timer _timer; // 定时器timer
  double _offset = 0.0; // 执行动画的偏移量

  @override
  void initState() {
    super.initState();

    _controller = ScrollController(initialScrollOffset: _offset);

    // _controller.animateTo(widget.stepOffset,
    // duration: widget.duration, curve: Curves.linear); // 线性曲线动画
    Future.delayed(Duration.zero, () {
      _controller.animateTo(widget.stepOffset,
          duration: widget.duration, curve: Curves.linear); // 线性曲线动画
    });
    _timer = Timer.periodic(widget.duration, (timer) {
      double newOffset = _controller.offset + widget.stepOffset;
      if (newOffset != _offset) {
        _offset = newOffset;
        // _controller.jumpTo(value)
        _controller.animateTo(_offset,
            duration: widget.duration, curve: Curves.linear); // 线性曲线动画
      }
    });
  }

  @override
  void dispose() {
    _timer.cancel();
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return CarouselSlider(
      options: CarouselOptions(autoPlay: true, scrollDirection: Axis.vertical),
      items: widget.children,
    );
  }
}
